<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>遍历</title>
  </head>
  <body>
    <ul>
      <li>keys()遍历返回键名</li>
      <li>values()遍历返回键值</li>
      <li>entries()遍历返回键值对</li>
      <li>forEach()使用回调函数遍历每个成员</li>
    </ul>
    <p>Set的键名和键值是相同的，因为Set没有键名</p>
    <pre>
          let set = new Set(["red", "green", "blue"]);
  for (let item of set.keys()) {
    console.log(item); //  'red'   'green'   'blue'
  }
  for (let item of set.values()) {
    console.log(item); //  'red'   'green'   'blue'
  }
  for (let item of set.entries()) {
    console.log(item); // ['red','red']  ['green','green']  ['blue','blue']
  }
  set.forEach((value,key,set)=>{
    console.log(value*2)
  })
    let newSet = new Set([1, 2, 3, 4]);
  newSet.forEach((value, key, newSet) => {
    console.log(value * 2); // 2 4 6 8
  });
    </pre>
    <p>扩展运算符也可以用于Set结构</p>
    <pre>
        console.log([...newSet]);  // [1,2,3,4]
        console.log([...set]);   // ["red", "green", "blue"]
      </pre
    >
    <pre>
           let a = new Set([1, 2, 3]);
           let b = new Set(["a", "b", "c"]);
        </pre
    >
    <p>并集</p>
    <pre>
          let union = new Set([...a],[...b])
        </pre
    >
    <p>交集</p>
    <pre>
          let intersect =  new Set([...a].filter(x=>b.has(x)))
        </pre
    >
    <p>差集</p>
    <pre>
          let difference =  new Set([...a].filter(x=>!b.has(x)))
        </pre
    >
  </body>
</html>
<script>
  let set = new Set(["red", "green", "blue"]);
  for (let item of set.keys()) {
    console.log(item); //  'red'   'green'   'blue'
  }
  for (let item of set.values()) {
    console.log(item); //  'red'   'green'   'blue'
  }
  for (let item of set.entries()) {
    console.log(item); // ['red','red']  ['green','green']  ['blue','blue']
  }
  let newSet = new Set([1, 2, 3, 4]);
  newSet.forEach((value, key, newSet) => {
    console.log(value * 2); // 2 4 6 8
  });
  console.log([...newSet]); // [1,2,3,4]
  console.log([...set]); // ["red", "green", "blue"]
  let a = new Set([1, 2, 3]);
  let b = new Set(["a", "b", "c"]);
  let union = new Set([...a], [...b]);
  let intersect = new Set([...a].filter((x) => b.has(x)));
  let difference = new Set([...a].filter((x) => !b.has(x)));
  console.log(union); // Set(3) {1,2,3}
  console.log(intersect); // Set(0) {}
  console.log(difference); //Set(3) {1,2,3}
</script>
